package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhangmeng
 * @description 检查组控制器
 * @date 2019/9/18
 **/
@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {

    @Reference
    private RoleService roleService;

    /**
     * 添加
     *
     * @return
     */
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    public Result add(@RequestBody Role role, Integer[] permissionIds) {
        log.info("[角色-添加]data:{},permissionIds:{}", role, permissionIds);
        //RPC请求添加
        roleService.add(role,permissionIds);
        return new Result(true, MessageConst.ADD_ROLE_SUCCESS);
    }


    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping("/findPage")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[角色-分页查询]data:{}", queryPageBean);
        //rpc查询结果
        PageResult pageResult = roleService.pageQuery(queryPageBean);
        //构造返回对象
        return new Result(true,MessageConst.QUERY_ROLE_SUCCESS,pageResult);

    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findById(Integer id) {
        log.info("[角色-根据id查询]id:{}", id);

        //rpc 调用业务处理
       Role  role =roleService.findById(id);
        //封装返回值
        return new Result(true, MessageConst.QUERY_ROLE_SUCCESS, role);

    }

    /**
     * 根据检查组ID查询关联的检查项ID集和
     *
     * @param id
     * @return
     */
    @RequestMapping("/findpermissionIdsByCheckRoleId")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findpermissionIdsByCheckRoleId(Integer id) {
        log.info("[角色-根据检查组ID查询关联的检查项ID集和]id:{}", id);

        //rpc 调用业务处理
        List<Integer> permissionIds = roleService.findpermissionIdsByCheckRoleId(id);
        //封装返回值
        return new Result(true, MessageConst.ACTION_SUCCESS, permissionIds);

    }

    /**
     * 编辑
     *
     * @return
     */
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    public Result edit(@RequestBody Role role, Integer[] permissionIds) {
        log.info("[角色-编辑]data:{},checkitemIds:{}", role, permissionIds);

        //RPC请求添加
        roleService.edit(role, permissionIds);
        return new Result(true, MessageConst.UPDATE_ROLE_SUCCESS);

    }

    /**
     * 查询所有
     *
     * @return
     */
    @RequestMapping("findAll")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findAll() {
        //rpc调用查询数据
        List<Role> role = roleService.findAll();
        //封装返回结果
        return new Result(true, MessageConst.QUERY_ROLE_SUCCESS, role);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    public Result delete(Integer id){
        log.info("[角色-根据id删除]id：{}",id);
        //RPC调用处理业务
        roleService.delete(id);
        return new Result(true,MessageConst.DELETE_ROLE_SUCCESS);
    }
}
